﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using CrystalDecisions.CrystalReports;
using System.Data.SqlClient;
using System.Globalization;

public partial class pages_page_print : System.Web.UI.Page
{
    CrystalDecisions.CrystalReports.Engine.ReportDocument myReport;
    public static rptDataSet ds = new rptDataSet();
    DataRow _dt_row = null;
    DataTable dtt;
    Common comm = new Common();
    
    DateTime _tungay = DateTime.Now;
    DateTime _denngay = DateTime.Now;

    public string messError = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        string id = "";
        
        if (Request.Params["print"] != null)
        {
            if (Request.Params["tungay"] != null)
            {
                try
                {
                    if (!Request.Params["tungay"].ToString().Trim().Equals(""))
                    {
                        //_tungay = Convert.ToDateTime(Request.Params["tungay"].ToString());
						_tungay = DateTime.ParseExact(Request.Params["tungay"].ToString(), "dd-MM-yyyy", null);
                        
                    }
                }
                catch { _tungay = DateTime.Now; }
            }
            if (Request.Params["dengay"] != null)
            {
                try
                {
                    if (!Request.Params["dengay"].ToString().Trim().Equals(""))
                    {
                        //_denngay = Convert.ToDateTime(Request.Params["denngay"].ToString());
						_denngay = DateTime.ParseExact(Request.Params["dengay"].ToString(), "dd-MM-yyyy", null);
                    }
                }
                catch { _denngay = DateTime.Now; }
            }
            switch (Request.Params["print"].ToString())
            {
                case "3b":
                    Print_3b();
                    break;
				case "3c":
                    Print_3c();
                    break;
            }
        }
    }
	
	void Print_3c()
    {
        try
        {
            //CrystalDecisions.CrystalReports.Engine.ReportDocument myReport = new CrystalDecisions.CrystalReports.Engine.ReportDocument();
            //Lấy dữ liệu cho báo cáo
            string Sql = "SELECT B.ma_ho_so,A.ma_doanh_nghiep,A.ten_doanh_nghiep,B.so_luong_tuyen,C.ten_nguon_vlt,"+
	            " A.dia_chi_chi_tiet,dbo.fcGetDiaChiChiTiet(A.ma_dia_chi) AS dia_chi,B.noi_lam_viec,"+
	            " B.luong_den,B.luong_tu,B.quyen_loi,B.ngay_du_tuyen,"+
                " B.ngay_het_han,B.mo_ta,b.yeu_cau_cong_viec,B.ngay_dang,B.vi_tri_tuyen_dung,D.ten_chuc_danh" +
                " FROM dbo.DoanhNghiep_doanh_nghiep AS A LEFT JOIN dbo.ViecLamTrong_ho_so_tuyen_dung AS B"+
                " ON A.ma_doanh_nghiep = B.ma_doanh_nghiep LEFT JOIN dbo.DanhMuc_nguon_viec_lam_trong AS C"+
                " ON B.ma_nguon_vlt = C.ma_nguon_vlt LEFT JOIN dbo.DanhMuc_chuc_danh AS D"+
                " ON B.ma_chuc_danh=D.ma_chuc_danh" +
                " WHERE (B.ngay_dang>='" + _tungay.ToString("MM-dd-yyyy") + "' AND B.ngay_dang<='" + _denngay.ToString("MM-dd-yyyy") + "')" +
                " and (B.ma_ho_so IS NOT NULL)"+
                " ORDER BY B.ngay_dang";
            dtt = comm.getDataTable(Sql);
            ds.baocao_3c.Clear();
            if (dtt.Rows.Count > 0)
            {
                int stt = 0; string ngay = DateTime.Parse(dtt.Rows[0]["ngay_dang"].ToString()).ToString("dd-MM-yyyy");
                for (int i = 0; i < dtt.Rows.Count; i++)
                {
                    if (DateTime.Parse(dtt.Rows[i]["ngay_dang"].ToString()).ToString("dd-MM-yyyy") != ngay)
                    {
                        stt = 1; ngay = DateTime.Parse(dtt.Rows[i]["ngay_dang"].ToString()).ToString("dd-MM-yyyy");
                    }
                    else { stt = stt + 1; }
                    _dt_row = ds.baocao_3c.Newbaocao_3cRow();
                    _dt_row["stt"] = stt;
                    _dt_row["ma_vtn"] = dtt.Rows[i]["ma_ho_so"].ToString();
                    _dt_row["vi_tri_tuyen_dung"] = dtt.Rows[i]["vi_tri_tuyen_dung"].ToString()+" - " + dtt.Rows[i]["ten_chuc_danh"].ToString();
                    _dt_row["nguon_viec_lam"] = dtt.Rows[i]["ten_nguon_vlt"].ToString();
                    _dt_row["so_luong_tuyen"] = dtt.Rows[i]["so_luong_tuyen"].ToString();
                    _dt_row["ten_doanh_nghiep"] = dtt.Rows[i]["ten_doanh_nghiep"].ToString();
                    _dt_row["dia_chi_chi_tiet"] = dtt.Rows[i]["dia_chi_chi_tiet"].ToString();
                    _dt_row["dia_chi"] = dtt.Rows[i]["dia_chi"].ToString();
                    _dt_row["muc_luong"] = dtt.Rows[i]["luong_tu"].ToString() + " - " + dtt.Rows[i]["luong_den"].ToString();
                    _dt_row["che_do_khac"] = dtt.Rows[i]["quyen_loi"].ToString();
                    _dt_row["thoi_han_tuyen"] =DateTime.Parse(dtt.Rows[i]["ngay_du_tuyen"].ToString()).ToString("dd/MM/yyyy")+
                       " - " + DateTime.Parse(dtt.Rows[i]["ngay_het_han"].ToString()).ToString("dd/MM/yyyy");
                    _dt_row["yeu_cau_khac"] = dtt.Rows[i]["yeu_cau_cong_viec"].ToString();
                    _dt_row["ghi_chu"] = dtt.Rows[i]["mo_ta"].ToString();
                    _dt_row["ngay_tao"] = dtt.Rows[i]["ngay_dang"].ToString();
                    string[] temp = dtt.Rows[i]["noi_lam_viec"].ToString().Split('#');
                    for(int k=0;k<temp.Length;k++)
                    {
                        _dt_row["dia_diem_lam_viec"] += comm.execScalar("select dbo.fcGetDiaChiChiTiet('" + temp[k].Trim() + "') as noi_lam_viec").ToString();
                    }
                    //hocj van chuyen mon
                    string subSQL = "SELECT B.ten_chuyen_mon FROM dbo.ViecLamTrong_yeu_cau_chuyen_mon AS A LEFT JOIN"+
	                    " dbo.DanhMuc_chuyen_mon AS B ON A.ma_chuyen_mon = B.ma_chuyen_mon" +
                        " where A.ma_ho_so='" + dtt.Rows[i]["ma_ho_so"].ToString() + "'";
                    DataTable dttsub = comm.getDataTable(subSQL);
                    _dt_row["trinh_do_chuyen_mon"] = "";
                    if (dttsub.Rows.Count > 0)
                    {
                        for (int j = 0; j < dttsub.Rows.Count; j++)
                        {
                            _dt_row["trinh_do_chuyen_mon"] += " - " + dttsub.Rows[j][0].ToString();
                        }
                    }
                    //nghiep vu
                    subSQL = "SELECT B.ten_nghiep_vu,C.ten_hang FROM dbo.ViecLamTrong_yeu_cau_nghiep_vu AS A LEFT JOIN" +
                        " dbo.DanhMuc_nghiep_vu AS B ON A.ma_nghiep_vu = B.ma_nghiep_vu" +
                        " LEFT JOIN dbo.DanhMuc_hang AS C ON A.ma_hang=C.ma_hang"+
                        " where A.ma_ho_so='" + dtt.Rows[i]["ma_ho_so"].ToString() + "' and " +
                        " (B.tin_hoc=1 OR B.ngoai_ngu=1 OR B.ma_nghiep_vu IS NULL)";
                    dttsub = comm.getDataTable(subSQL);
                    _dt_row["th_nn"] = "";
                    if (dttsub.Rows.Count > 0)
                    {
                        for (int j = 0; j < dttsub.Rows.Count; j++)
                        {
                            _dt_row["th_nn"] += " - " + dttsub.Rows[j][0].ToString() + "(" + dttsub.Rows[j][1].ToString() + ")";
                        }
                    }


                    ds.baocao_3c.Rows.Add(_dt_row);
                }
            }

            myReport = new CrystalDecisions.CrystalReports.Engine.ReportDocument();
            myReport.Load(Server.MapPath("03c.rpt"));

            myReport.SetDataSource(ds);
            CrystalReportViewer1.ReportSource = myReport;
        }
        catch (Exception er)
        {
            messError += er.Message.ToString();
        }
    }
    void Print_3b()
    {
        try
        {
            CrystalDecisions.CrystalReports.Engine.ReportDocument myReport = new CrystalDecisions.CrystalReports.Engine.ReportDocument();
            myReport.Load(Server.MapPath("03b.rpt"));
            //Lấy dữ liệu cho báo cáo
            string Sql = "SELECT A.ma_nguoi,A.ho_ten,A.ngay_sinh,A.gioi_tinh,A.cmnd," +
                    " A.dia_chi_chi_tiet,dbo.fcGetDiaChiChiTiet(A.dia_chi) AS dia_chi,A.ngay_tao" +
                    " FROM dbo.ConNguoi_nguoi AS A" +
                    " WHERE (A.ngay_tao>='" + _tungay.ToString("MM/dd/yyyy") + "' AND A.ngay_tao<='" + _denngay.ToString("MM/dd/yyyy") + "')" +
                    " ORDER BY A.ngay_tao";
            dtt = comm.getDataTable(Sql);
            ds.baocao_3b.Clear();
            if (dtt.Rows.Count > 0)
            {
                int stt = 0; 
                string ngay = DateTime.Parse(dtt.Rows[0]["ngay_tao"].ToString()).ToString("dd-MM-yyyy");
                for (int i = 0; i < dtt.Rows.Count; i++)
                {
                    if (DateTime.Parse(dtt.Rows[i]["ngay_tao"].ToString()).ToString("dd-MM-yyyy") != ngay)
                    {
                        stt = 1;
                        //ngay = DateTime.Parse(dtt.Rows[i]["ngay_tao"].ToString()).ToString("dd-MM-yyyy");
                    }
                    else { stt = stt + 1; }

                    _dt_row = ds.baocao_3b.Newbaocao_3bRow();
                    _dt_row["stt"] = stt;
                    _dt_row["ma_nguoi"] = dtt.Rows[i]["ma_nguoi"].ToString();
                    _dt_row["ho_ten"] = dtt.Rows[i]["ho_ten"].ToString();
                    _dt_row["ngay_sinh"] = dtt.Rows[i]["ngay_sinh"].ToString();
                    _dt_row["gioi_tinh"] = dtt.Rows[i]["gioi_tinh"].ToString();
                    _dt_row["cmnd"] = dtt.Rows[i]["cmnd"].ToString();
                    _dt_row["dia_chi_chi_tiet"] = dtt.Rows[i]["dia_chi_chi_tiet"].ToString();
                    _dt_row["dia_chi"] = dtt.Rows[i]["dia_chi"].ToString();
                    _dt_row["ngay_tao"] = dtt.Rows[i]["ngay_tao"].ToString();
                    //hocj van chuyen mon
                    string subSQL = "select C.ten_hoc_van from ConNguoi_nguoi as A left join ConNguoi_hoc_van_chuyen_mon as B" +
                        " on A.ma_nguoi=B.ma_nguoi left join DanhMuc_hoc_van as C on B.ma_hoc_van=C.ma_hoc_van" +
                        " where A.ma_nguoi='" + dtt.Rows[i]["ma_nguoi"].ToString() + "'";
                    DataTable dttsub = comm.getDataTable(subSQL);
                    _dt_row["chuyen_mon"] = "";
                    if (dttsub.Rows.Count > 0)
                    {
                        for (int j = 0; j < dttsub.Rows.Count; j++)
                        {
                            _dt_row["chuyen_mon"] += " - " + dttsub.Rows[j][0].ToString();
                        }
                    }
                    //nghiep vu
                    subSQL = "select C.ten_nghiep_vu from ConNguoi_nguoi as A left join ConNguoi_nghiep_vu as B" +
                        " on A.ma_nguoi=B.ma_nguoi left join DanhMuc_nghiep_vu as C on B.ma_nghiep_vu=C.ma_nghiep_vu" +
                        " where A.ma_nguoi='" + dtt.Rows[i]["ma_nguoi"].ToString() + "' and " +
                        " (C.tin_hoc=1 OR C.ngoai_ngu=1 OR C.ma_nghiep_vu IS NULL)";
                    dttsub = comm.getDataTable(subSQL);
                    _dt_row["th_nn"] = "";
                    if (dttsub.Rows.Count > 0)
                    {
                        for (int j = 0; j < dttsub.Rows.Count; j++)
                        {
                            _dt_row["th_nn"] += " - " + dttsub.Rows[j][0].ToString();
                        }
                    }
                    //kha nang khac=kha nang mem
                    subSQL = "SELECT C.ten_kn_mem FROM dbo.ConNguoi_nguoi AS A LEFT JOIN dbo.ConNguoi_kha_nang_mem AS B" +
                        " ON A.ma_nguoi = B.ma_nguoi LEFT JOIN dbo.DanhMuc_kha_nang_mem AS C ON B.ma_kn_mem = C.ma_kn_mem" +
                        " where A.ma_nguoi='" + dtt.Rows[i]["ma_nguoi"].ToString() + "'";
                    dttsub = comm.getDataTable(subSQL);
                    _dt_row["kha_nang_khac"] = "";
                    if (dttsub.Rows.Count > 0)
                    {
                        for (int j = 0; j < dttsub.Rows.Count; j++)
                        {
                            _dt_row["kha_nang_khac"] += " - " + dttsub.Rows[j][0].ToString();
                        }
                    }
                    //Hồ sơ tìm việc
                    subSQL = "SELECT C.ten_chuc_danh,B.muc_luong_mong_muon,B.mo_ta,B.ma_ho_so " +
                        " FROM dbo.ConNguoi_nguoi AS A LEFT JOIN dbo.NguoiTimViec_ho_so AS B" +
                        " ON A.ma_nguoi = B.ma_nguoi LEFT JOIN dbo.DanhMuc_chuc_danh AS C" +
                        " ON B.chuc_danh_mong_muon=C.ma_chuc_danh" +
                        " where A.ma_nguoi='" + dtt.Rows[i]["ma_nguoi"].ToString() + "'";
                    dttsub = comm.getDataTable(subSQL);
                    _dt_row["vi_tri_cong_viec"] = ""; _dt_row["muc_luong"] = "0"; _dt_row["yeu_cau_khac"] = "";
                    if (dttsub.Rows.Count > 0)
                    {
                        _dt_row["vi_tri_cong_viec"] = dttsub.Rows[0][0].ToString();
                        _dt_row["muc_luong"] = dttsub.Rows[0][1].ToString();
                        _dt_row["yeu_cau_khac"] = dttsub.Rows[0][2].ToString();
                        subSQL = "SELECT dbo.fcGetDiaChiChiTiet(B.ma_dia_chi) AS tplv " +
                            " FROM dbo.NguoiTimViec_ho_so AS A LEFT JOIN dbo.NguoiTimViec_thanh_pho_lam_viec AS B" +
                            " ON A.ma_ho_so = B.ma_ho_so" +
                            " where A.ma_ho_so='" + dttsub.Rows[0][3].ToString() + "'";
                        DataTable tt = comm.getDataTable(subSQL);
                        _dt_row["noi_lam_viec"] = "";
                        if (tt.Rows.Count > 0)
                        {
                            _dt_row["noi_lam_viec"] += " - " + tt.Rows[0][0].ToString();
                        }
                    }
                    ds.baocao_3b.Rows.Add(_dt_row);
                }
            }

            //ds.dulieu.Merge(dtt);

            myReport.SetDataSource(ds);

            CrystalReportViewer1.ReportSource = myReport;
        }
        catch (Exception er)
        {
            messError += er.Message.ToString();
        }
    }

    protected void CrystalReportViewer1_Unload(object sender, EventArgs e)
    {
        try
        {
            myReport.Close();
            myReport.Dispose();
        }catch{}
    }
}

